#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main() {
    struct sockaddr_in serv_addr;
    char str[64] = {0};
    char rstr[64] = {0};
    strstr("hellasdjlfk\n", "\n");

    // configure IP and port;
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv_addr.sin_port = htons(1234);

    while(1) {
        // create a socket
        int sock_fd = socket(AF_INET, SOCK_STREAM, 0);

        // connet to server;
        connect(sock_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));

        // send and rewrite!
        fgets(str, sizeof(str) - 1, stdin);
        write(sock_fd, str, sizeof(str));
        read(sock_fd, rstr, sizeof(rstr));
        printf("ECHO: %s", rstr);

        if(!strcmp("exit\n", rstr)) {
            close(sock_fd);
            break;
        }

        close(sock_fd);
    }
    return 0;
    
}
